package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"shop/models"
	"strconv"
	"time"
)

type ShopController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *ShopController) Add() {
	name := c.GetString("name")
	status := c.GetString("status")
	img := c.GetString("img")
	cate := c.GetString("cate")
	area := c.GetString("area")
	shop := models.Shop{
		Name:   name,
		Status: status,
		Img:    img,
		Cate:   cate,
		Area:   area,
	}
	if name == "" || area == "" {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "店铺名称、店铺地址为必填项",
			Data: nil,
		}
		c.ServeJSON()
	}
	err := models.DB.Create(&shop).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: shop,
		}
		c.ServeJSON()
	}
}

func (c *ShopController) Upload() {
	file, header, err := c.GetFile("img")

	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "请选择文件并上传",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()

	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "图片大小必须为2MB以内",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	path := "static/img/" + header.Filename
	c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		img := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "上传成功",
			Data: img,
		}
		c.ServeJSON()
	}
}

func (c *ShopController) List() {
	pageData := make(map[string]interface{})
	query := models.DB.Model(&models.Shop{})

	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)

	key := "shop-" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()

	if err != nil {
		var count int64
		query.Count(&count)

		offset := (page - 1) * size
		var shop []models.Shop

		query = models.DB.Model(&models.Shop{})
		name := c.GetString("name")
		if name == "" {
			query = query.Where("name like?", "%"+name+"%")
		}

		err = query.Offset(offset).Limit(size).Find(&shop).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			pageData["shop"] = shop
			pageData["count"] = count
			jsonByte, _ := json.Marshal(&pageData)
			value := string(jsonByte)
			models.Rdb.Set(key, value, time.Second*5)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "查询成功",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}
}

func (c *ShopController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.Shop{}, id).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

func (c *ShopController) Info() {
	id, _ := c.GetInt("id")

	key := "shop-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()

	var shop models.Shop

	if err != nil {
		err = models.DB.Where("ID=?", id).First(&models.Shop{}).Error

		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "展示失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&shop)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "展示成功-mysql",
				Data: shop,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &shop)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "展示成功-redis",
			Data: shop,
		}
		c.ServeJSON()
	}
}
